import time

from unsplash_spider_image.tag_baidu_translator import convert_to_chinese
from setting.mysql_test import mysql45_config
import pymysql

mysql_conn = pymysql.connect(**mysql45_config)
mysql_cursor = mysql_conn.cursor()

def update_chinese_tag():
    #select_sql = "SELECT id, tag FROM tag_info WHERE id > 1116"
    select_sql = "SELECT id, tag FROM tag_info WHERE id > 11548 "
    mysql_cursor.execute(select_sql)
    tag_list = mysql_cursor.fetchall()
    for i, tags in enumerate(tag_list):
        tag_id = tags[0]
        tag = tags[1]
        tag = tag.replace("'", "")  # 将单引号替换为空
        #print(i, 'id', tag_id, 'tag', tag)
        chinese_tag = convert_to_chinese(tag)
        print(i, 'id', tag_id, 'tag', tag, 'chinese_tag', chinese_tag)
        time.sleep(1)
        update_sql = "UPDATE tag_info SET chinese_tag='%s' WHERE id='%s'" % (chinese_tag, tag_id)
        mysql_cursor.execute(update_sql)
        mysql_conn.commit()


if __name__ == '__main__':
    update_chinese_tag()
    mysql_cursor.close()
    mysql_conn.close()
